package com.lanou.myim.ui.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;


import com.easemob.EMValueCallBack;
import com.easemob.chat.EMGroup;
import com.easemob.chat.EMGroupManager;
import com.lanou.myim.R;
import com.lanou.myim.adapter.FragmentConversationAdapter;
import com.lanou.myim.entity.EventBusConstant;
import com.lanou.myim.entity.GroupsEty;
import com.lanou.myim.entity.MsgEty;

import java.util.List;

import com.lanou.myim.presenter.ConversationPresenter;
import com.lanou.myim.presenter.ConversationPresenterInterface;


import de.greenrobot.event.EventBus;
import de.greenrobot.event.Subscribe;
import de.greenrobot.event.ThreadMode;

/**
 * Created by dllo on 16/3/3.
 */
public class ConversationFragment extends BaseFragment implements ConversationFragmentInterface {
    private EditText seek;
    private FragmentConversationAdapter adapter;
    private ListView listView;
    private ConversationPresenterInterface conversationPresenter;
    private Handler handler;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        EventBus.getDefault().register(this);
        View view = inflater.inflate(R.layout.fragment_conversation, null);
        seek = (EditText) view.findViewById(R.id.et_conversation_seek);
        listView = (ListView) view.findViewById(R.id.conversation_listView);
        conversationPresenter = new ConversationPresenter(this);
        EMGroupManager.getInstance().asyncGetGroupsFromServer(new EMValueCallBack<List<EMGroup>>() {
            @Override
            public void onSuccess(List<EMGroup> emGroups) {
                Message message = new Message();
                GroupsEty groupsEty = new GroupsEty();
                groupsEty.setGroups(emGroups);
                message.obj = groupsEty;
                handler.sendMessage(message);
            }

            @Override
            public void onError(int i, String s) {

            }
        });
        handler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message msg) {
                List<EMGroup> groups = ((GroupsEty)msg.obj).getGroups();
                adapter = new FragmentConversationAdapter(getActivity(),conversationPresenter,groups);
                listView.setAdapter(adapter);
                return false;
            }
        });
                seek.addTextChangedListener(new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                    }


            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                String changed = seek.getText().toString();
                conversationPresenter.searchMsgs(changed);
            }


                    @Override
                    public void afterTextChanged(Editable editable) {

                    }
                });

        return view;
    }



    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    //主界面收到新消息提醒
    @Subscribe(threadMode = ThreadMode.MainThread)
    public void onMainEvent(MsgEty msgEty) {
        conversationPresenter.insertMsg(msgEty);
    }
    //通知主界面刷新未读消息
    @Subscribe(threadMode = ThreadMode.MainThread)
    public void onMainEvent(Integer i) {
        if (i == EventBusConstant.REFRESH_UNREAD_COUNT||i == EventBusConstant.REFRESH_REMARKS)
        refreshUi(conversationPresenter.getMsgList());
    }


    @Override
    public void refreshUi(List<MsgEty> msgEtyList) {
        adapter.refresh(msgEtyList);
    }


}




